package com.beeweeb.services.tps.clustering;

import org.jgroups.Address;
import org.jgroups.ReceiverAdapter;
import org.jgroups.View;
import org.jgroups.stack.IpAddress;
import org.jgroups.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author guido d'albore
 */
public class UdpClusterReceiver extends ReceiverAdapter {
    Logger  logger = LoggerFactory.getLogger("ThreadPoolScheduler");
    
    @Override
    public void viewAccepted(View view) {
        super.viewAccepted(view);

        logger.info("Cluster members list has changed:");

        int i = 0;
        String address;

        for(Address a : view.getMembers()) {
            if(a instanceof UUID) {
                address = ((UUID)a).toStringLong().toUpperCase();
            } else {
                if(a instanceof IpAddress) {
                    address = ((IpAddress)a).getIpAddress().getHostAddress();
                } else {
                    address = "";
                }
            }

            logger.info(String.format("\t[%d] Member name: %s, %s (%s)", i, a.toString(), address, ((i == 0) ? "active" : "passive")));
            i++;
        }
    }
}
